package experiment3.shudu001.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.view.View;

import experiment3.shudu001.R;
import experiment3.shudu001.models.PrefsManager;

/**
 * Created by HP on 2025/4/26.
 */

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        CheckBox highlightCheckBox = (CheckBox) findViewById(R.id.highlightCheckBox);
        CheckBox mistakesCheckBox = (CheckBox) findViewById(R.id.mistakesCheckBox);
        RadioGroup themeRadioGroup = (RadioGroup) findViewById(R.id.themeRadioGroup);

        // 加载当前设置
        highlightCheckBox.setChecked(PrefsManager.getHighlightEnabled(this));
        mistakesCheckBox.setChecked(PrefsManager.getMistakesEnabled(this));
        int theme = PrefsManager.getTheme(this);
        if (theme == R.style.AppTheme_Dark) {
            themeRadioGroup.check(R.id.darkThemeRadio);
        } else {
            themeRadioGroup.check(R.id.lightThemeRadio);
        }

        // 设置监听器
        highlightCheckBox.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
                PrefsManager.setHighlightEnabled(SettingsActivity.this, isChecked);
            }
        });

        mistakesCheckBox.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
                PrefsManager.setMistakesEnabled(SettingsActivity.this, isChecked);
            }
        });

        themeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.darkThemeRadio) {
                    PrefsManager.setTheme(SettingsActivity.this, R.style.AppTheme_Dark);
                } else {
                    PrefsManager.setTheme(SettingsActivity.this, R.style.AppTheme_Light);
                }
                recreate(); // 重新创建Activity以应用新主题
            }
        });

        findViewById(R.id.backButtonSettings).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}